/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.adv.service;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import com.thinkgem.jeesite.common.aliyuncs.AliyuncsUtil;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.service.ServiceException;
import com.thinkgem.jeesite.common.utils.ImgUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkAllAdv;

import com.thinkgem.jeesite.modules.hk.sys.entity.adv.HkCategory;
import com.thinkgem.jeesite.modules.hk.sys.service.adv.HkCategoryService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkCityAdv;
import com.thinkgem.jeesite.modules.hk.adv.dao.HkCityAdvDao;
import org.springframework.web.util.UriUtils;

/**
 * 城市广告管理Service
 *
 * @author 罗天文
 * @version 2018-03-20
 */
@Service
@Transactional(readOnly = true)
public class HkCityAdvService extends CrudService<HkCityAdvDao, HkCityAdv> {
    @Autowired
    HkAllAdvService hkAllAdvService;
    @Autowired
    HkCategoryService hkCategoryService;

    public HkCityAdv get(String id) {
        return super.get(id);
    }

    public List<HkCityAdv> findList(HkCityAdv hkCityAdv) {
        return super.findList(hkCityAdv);
    }

    public Page<HkCityAdv> findPage(Page<HkCityAdv> page, HkCityAdv hkCityAdv) {
        return super.findPage(page, hkCityAdv);
    }

    @Transactional(readOnly = false)
    public void save(HkCityAdv hkCityAdv) {
        if (null != hkCityAdv.getNote()) {
            hkCityAdv.setNote(StringEscapeUtils.unescapeHtml4(hkCityAdv.getNote()));
        }

        hkCityAdv.setTag(null);
        if (null != hkCityAdv.getTags()) {
            hkCityAdv.setTag(String.join(",", hkCityAdv.getTags()));
        }
        String coverImgSrc = hkCityAdv.getCoverImgSrc();
        if (!coverImgSrc.contains(Global.getFileServer())) {
            coverImgSrc = (Global.getFileServer().trim() + hkCityAdv.getCoverImgSrc());
            Map<String, String> map = ImgUtils.getImgWidthHeight(hkCityAdv.getCoverImgSrc());
            hkCityAdv.setCoverImgWidth(map.get("width"));
            hkCityAdv.setCoverImgHeight(map.get("height"));
        }
        if (StringUtils.isNotEmpty(hkCityAdv.getAimgSrc())) {

            String medisPath = Global.getUserfilesBaseDir() + hkCityAdv.getAimgSrc();
            try {
                medisPath = UriUtils.decode(medisPath, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw new ServiceException("上传失败");
            }
            StringBuffer sb = new StringBuffer();
            HkCategory category;
            for (String c : hkCityAdv.getCategory().split(",")
            ) {
                category = hkCategoryService.get(c);
                sb.append(category.getName()).append(",");
            }
            String videoId = AliyuncsUtil.uploadVideo(hkCityAdv.getTitle(), medisPath, coverImgSrc, sb.toString());
            if (StringUtils.isEmpty(videoId)) {
                throw new ServiceException("上传失败");
            }
            hkCityAdv.setImgSrc(videoId);
        }
        super.save(hkCityAdv);

        deleteTag(hkCityAdv);
        insertTag(hkCityAdv);
        deleteCategory(hkCityAdv);
        insertCategory(hkCityAdv);
        if (hkCityAdv.getState().equals("1")) {
            insertAllAdv(hkCityAdv);
        }
    }

    @Transactional(readOnly = false)
    public void delete(HkCityAdv hkCityAdv) {
        super.delete(hkCityAdv);
    }


    /**
     * 删除标签
     *
     * @param hkCityAdv
     * @return
     */
    private void deleteCategory(HkCityAdv hkCityAdv) {
        super.dao.deleteCategory(hkCityAdv);
    }

    /**
     * 增加标签
     *
     * @param hkCityAdv
     * @return
     */
    private void insertCategory(HkCityAdv hkCityAdv) {
        super.dao.insertCategory(hkCityAdv);
    }

    //同步数据
    private void insertAllAdv(HkCityAdv hkCityAdv) {
        String sourceId = hkCityAdv.getId();
        HkAllAdv hkAllAdvOld = hkAllAdvService.getBySourceId(sourceId);
        if (null == hkAllAdvOld) {
            HkAllAdv hkAllAdv = new HkAllAdv();
            BeanUtils.copyProperties(hkCityAdv, hkAllAdv);
            hkAllAdv.setId(null);
            hkAllAdv.setSourceId(hkCityAdv.getId());
            //类型  1自媒体 2企业广告3城市广告4传统文化
            hkAllAdv.setType("3");
            hkAllAdvService.save(hkAllAdv);
        } else {
            HkAllAdv hkAllAdv = hkAllAdvOld;
            BeanUtils.copyProperties(hkCityAdv, hkAllAdv);
            hkAllAdv.setId(hkAllAdvOld.getId());
            hkAllAdvService.save(hkAllAdv);
        }

        hkAllAdvService.deleteTag(hkCityAdv.getId(), hkCityAdv.getTag());
        hkAllAdvService.insertTag(hkCityAdv.getId(), hkCityAdv.getTag());
        hkAllAdvService.deleteCategory(hkCityAdv.getId(), hkCityAdv.getCategory());
        hkAllAdvService.insertCategory(hkCityAdv.getId(), hkCityAdv.getCategory());
    }

    /**
     * 删除标签
     *
     * @param hkCityAdv
     * @return
     */
    private void deleteTag(HkCityAdv hkCityAdv) {
        super.dao.deleteTag(hkCityAdv);
    }

    /**
     * 增加标签
     *
     * @param hkCityAdv
     * @return
     */
    private void insertTag(HkCityAdv hkCityAdv) {
        //super.dao.insertTag(hkCityAdv);
    }
}